package com.flyduck.mall.pms.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.pms.api.constant.ProductConstant;
import com.flyduck.mall.pms.domain.PmsAd;
import com.flyduck.mall.pms.domain.PmsCategory;
import com.flyduck.mall.pms.domain.PmsProduct;
import com.flyduck.mall.pms.h5.service.H5HomeService;
import com.flyduck.mall.pms.h5.vo.H5HomeDataRespVO;
import com.flyduck.mall.pms.mapper.PmsAdMapper;
import com.flyduck.mall.pms.mapper.PmsCategoryMapper;
import com.flyduck.mall.pms.mapper.PmsProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-05-18 18:49
 **/
@Service
public class H5HomeServiceImpl implements H5HomeService {

    @Resource
    private PmsAdMapper pmsAdMapper;
    @Resource
    private PmsCategoryMapper pmsCategoryMapper;
    @Resource
    private PmsProductMapper pmsProductMapper;

    @Override
    public H5HomeDataRespVO getHomeData() {
        H5HomeDataRespVO h5HomeDataRespVO = new H5HomeDataRespVO();

        // 首页轮播图
        List<PmsAd> pmsAdList = pmsAdMapper.selectList(
                new LambdaQueryWrapper<PmsAd>()
                .eq(PmsAd::getPosition, ProductConstant.AppAdPosition.carousel.getCode())
                .eq(PmsAd::getIsShow,true)
                .orderByDesc(PmsAd::getSortOrder)
        );
        List<H5HomeDataRespVO.PmsAdVO> appAdVOList = BeanUtils.toBeanList(pmsAdList, H5HomeDataRespVO.PmsAdVO.class);
        h5HomeDataRespVO.setCarousels(appAdVOList);


        // 分类数据
        List<PmsCategory> pmsCategoryList = pmsCategoryMapper.selectList(
                new LambdaQueryWrapper<PmsCategory>()
                .eq(PmsCategory::getIsShow,true)
                .orderByDesc(PmsCategory::getSortOrder)
        );
        List<H5HomeDataRespVO.PmsCategoryVO> categories = BeanUtils.toBeanList(pmsCategoryList, H5HomeDataRespVO.PmsCategoryVO.class);
        h5HomeDataRespVO.setCategories(categories);


        // 置顶或者热销商品
        List<PmsProduct> pmsProductList = pmsProductMapper.selectList(
                new LambdaQueryWrapper<PmsProduct>()
                        .eq(PmsProduct::getIsOnSale, true)
                        .and(wrapper -> wrapper.eq(PmsProduct::getIsHot, true).or().eq(PmsProduct::getIsTop, true))
                        .orderByDesc(PmsProduct::getSortOrder)
                        .last("limit 10")
        );
        List<H5HomeDataRespVO.PmsProductVO> appProductVOS = BeanUtils.toBeanList(pmsProductList, H5HomeDataRespVO.PmsProductVO.class);
        h5HomeDataRespVO.setProducts(appProductVOS);
        return h5HomeDataRespVO;
    }
}
